﻿@using RBS.Web.Controllers
@model RBS.Core.Model.Hotel.H_Hotels
@{
    ViewBag.Title = "订单详情";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var hId = Model.H_id;
    var uid = -1;
    if (Request.IsAuthenticated)
    {
        uid = Convert.ToInt32(Context.User.Identity.Name);
    } 
}

<div id="content">
    <div class="h1a">本地商户</div>
    <div class="detailinfo">
        <h1>@Model.H_Name</h1>
        <dl class="fav">
            <dd class="fav1">@Html.ActionLink("收藏", "AddHotle", "Collect", new { Area = "User", hotelid = @Model.H_id }, null)</dd>
            @*	<dd class="share">   @Html.ActionLink("预定","Order",new{hotelid=@Model.H_id},null )</dd>*@
        </dl>
        <dl class="score">
            <dd class="add">地址：<b>@Model.Address</b></dd>
            <dd>电话：<b>@Model.Telphone</b></dd>
            <dd>人均：<strong>￥50</strong></dd>
            <dd>营业时间：<b>10:00-22:00</b></dd>
        </dl>
    </div>
    <div id="mainContent">
        <dl class="dtlcateg">
            <dd class="active"><a href="#">菜单</a></dd>
        </dl>
        <dl id="CategoryUL" class="dtlcategdtl">
            <dd class="active">
                <a href="#">全部</a>
            </dd>
        </dl>

        <dl class="dtlorderby">
            <dd class="active"><a href="#">默认</a></dd>
            <dd><a href="#">价格</a></dd>
            <dd><a href="#BtnNextOrder">热度</a></dd>
            <dd><a href="#">评价数</a></dd>
            <dd><a href="#">好评率</a></dd>
        </dl>
        <div class="maindishlist">
            <ol id="CookbookUL">
            </ol>
        </div>

    </div>
    <div id="sideContent">
        <div class="mymenu">
            <h2>我的菜单</h2>
            <div>
                <ul id="orderUL">
                </ul>
              
               <div class="total-warp">
					<a href="javascript:void(0);" class="J_DeletCartData"  onclick="DeleteOrderTemp(@ViewBag.OrderId,2)">清空</a>
					<dl class="price">
						<dd class="item-count">合计数量：<b id="numcount"></b>份</dd>
						<dd class="item-tit">合计金额：<b>¥</b> <strong id="priceCount"></strong></dd>
						<dd class="item-label">节省：<b>¥</b> <strong>20</strong></dd>
						<dd class="price-other">菜单金额以实际消费为准</dd>
					</dl>
					<a class="next-btn" href="#" id="BtnNextOrder">下一步</a>
				</div>
            </div>
        </div>
    </div>
</div>
<script>
    var orderid =@ViewBag.OrderId; 
    //获取菜谱分类
    function GetCategory(hid) {
        var url = "@Url.Action("GetCategory")?hid=" + hid; 
        $.ajax({
            url:url,
            type: 'Post',
            dataType: 'json',
            success: function (result) {
            
                if (result == null)
                    return;
                for (var i = 0; i < result.Data.length  ; i++) {
                    var dd = document.createElement('dd');

                    var a = document.createElement('a');
                    $(a).html(result.Data[i].Name);

                    $(dd).attr('cid', result.Data[i].Id);
                    /*  alert(cid);*/
                    $(dd).click(function () {
                        /*window.location = ($(this).attr('temurl'));*/
                        $("#CookbookUL").empty();
                        GetCookbook(hid, $(this).attr('cid'));
                    });
                    $(dd).append(a);
                    document.getElementById('CategoryUL').appendChild(dd);
                }
            }
        }); 
    }
    //显示菜单
    function GetCookbook(hid, cid) {
        var url = "@Url.Action("GetCookbook")?type=Cookbook&hid=" + hid + "&cid=" + cid; 
        $.ajax({
            url: url,
            type: 'Post',
            dataType: 'json',
            success: function (result) {
                if (result.Data.length==0) {
                 
                    var li = document.createElement('li');
                    var msg = document.createElement('div');
                    $(msg).attr("style", "float:right;width:60%");
                    $(msg).html("本类暂无菜单");
                    $(li).append(msg);
                    document.getElementById('CookbookUL').appendChild(li);
                    return;
                } 
                for (var i = 0; i < result.Data.length  ; i++) {
                
                    var li = document.createElement('li'); 
                    // 菜名
                    var imgdiv = document.createElement('div');
                    var img = document.createElement('img');
                    
                    $(imgdiv).attr("class", "mainimg");
                    $(img).attr("src", "http://gdd.alicdn.com/bao/uploaded/i1/TB1Mlr1GpXXXXX9XFXXXXXXXXXX_.jpg_240x240.jpg");
                    $(img).attr("alt",result.Data[i].Name);
                    
                    $(imgdiv).append(img);
                    var h3 = document.createElement('h3');
                    $(h3).html(result.Data[i].Name);
                    

                    var score = document.createElement('dl');
                    $(score).attr("class", "score");
                    
                    var dd1 = document.createElement('dd');
                    var dd2 = document.createElement('dd');
                    $(dd1).attr("class", "money");
                    $(dd2).attr("class", "sales");
                    $(dd1).html("￥<strong>"+ result.Data[i].Price+"</strong>");
                    $(dd2).html("销量：<b>215</b>");

                    $(score).append(dd1);
                    $(score).append(dd2); 
                    var p = document.createElement('p');
                    $(p).attr("class", "setu");
                    $(p).html("<a href=\"#\">点菜</a>");
                   
                    $(p).attr('Id',result.Data[i].Id);
                    $(p).attr('Name', result.Data[i].Name);
                    $(p).attr('Price', result.Data[i].Price);
                    $(p).click(function () { 
                        if (@uid==-1) { 
                            var url = "@Url.Action("Index", "Login", new { redirectUrl = Request.Url })";
                            window.location = url;
                        } else {
                            AddOrder($(this).attr('Id'),$(this).attr('Name'),$(this).attr('Price'),1,orderid);
                        }  
                    });  
                    $(li).append(imgdiv);
                    $(li).append(h3);
                    $(li).append(score);
                    $(li).append(p);
                    document.getElementById('CookbookUL').appendChild(li);
                }
            }
        });
    }
    //添加到菜谱
    function AddOrder(a, b, c,d,e) {  
        var tempurl = "@Url.Action("OrderTempDetail", "Order", new { Area = "User" })";
        $.ajax({
            url: tempurl,
            cache: false,
            type: 'POST',
            dataType: "json", 
            data: {
                CName: b,
                Price: c,
                HCId: a,
                Num: d,
                IsGuest: 0,
                UId:@uid,
                OrderId:e,
                hid:@Model.H_id,
                hname:'@Model.H_Name'
            },
            success: function (result) { 
             
                if (result.Data == null)
                    return; 
                
              
                $("#orderUL").empty();
                var numCount = 0;
                var priceCount = 0;
                $.each(result.Data, function (index, row) {
                    priceCount += row.Price*row.Num;
                    numCount += row.Num;
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                });
                
                $("#priceCount").html(priceCount);
                $("#numcount").html(numCount);
             
            }
        });
    }

    function GetOrderTempDetail(uid,oid) {
        var url = "@Url.Action("GetOrderTempDetail", "Order", new { Area = "User" })?uid=" + uid+"&oid="+oid; 
        $.ajax({
            url:url,
            type: 'Post',
            dataType: 'json',
            success: function (result) {

                $("#orderUL").html("");
                if (result == null)
                    return;

                var numCount = 0;
                var priceCount = 0;
                $.each(result.Data, function (index, row) { 
                    priceCount += row.Price*row.Num;
                    numCount += row.Num;
                   
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                });

                $("#priceCount").html(priceCount);
                $("#numcount").html(numCount);
             
            }
        }); 
    }
    

    function DeleteOrderTemp(a, b) {  
        var tempurl = "@Url.Action("DeleteOrderTempDetail", "Order", new { Area = "User" })";
         $.ajax({
             url: tempurl,
             cache: false,
             type: 'POST',
             dataType: "json", 
             data: {
                 id:a,
                 type:b 
            },
            success: function (result) { 
              
                    GetOrderTempDetail(@uid,orderid);
            }
        });
    }
    
    $(function () {
        GetCategory(@hId);
        GetCookbook(@hId,0);
        GetOrderTempDetail(@uid,orderid);
        $("#BtnNextOrder").click(function() { 
            var tempurl = "@Url.Action("UpdateOrder")?orderid="+orderid;
            $.ajax({
                url: tempurl,
                cache: false,
                type: 'POST',
                dataType: "json",  
                success: function (result) {  
                    if (result.Success == true) {
                        var url = "@Url.Action("OrderList", "Order", new { area = "User" })?hid="+@hId+"&orderid="+orderid;
                        window.location = url;
                    }
            }
         });
            


         
        });
    });
</script>
 <script type="text/template" id="OrderTemp">  
    <li> 
	    <div class="food-count-del"><a href="javascript:void(0);" data-change="-1" onclick="DeleteOrderTemp({{Id}},1)">×</a></div>
	    <div class="food-count-name">{{CName}}</div>
	    <div class="food-count-price">{{Price}}</div>
	    <div class="food-count-reduce"  ><a href="javascript:void(0);" data-change="-1" onclick="AddOrder({{HCId}},'{{CName}}',{{Price}},-1,{{OrderId}});">-</a></div>
	    <div class="food-count-num">{{Num}}</div>
	    <div class="food-count-add"><a href="javascript:void(0);" data-change="1" onclick="AddOrder({{HCId}},'{{CName}}',{{Price}},1,{{OrderId}});">+</a></div>
    </li>
</script>
